<?php
class AtomWidget extends CWidget
{
	public $title;
	public $cssClass='portlet';
	public $headerCssClass='header';
	public $contentCssClass='content';
	public $visible = true;

	public function __construct()
	{
		parent::__construct();
//		$this->visible = $this->applyVisibilityRules();
	}
//	protected function visibilityRules()
//	{
//		return array();
//	}

	protected function beforePrepareContent() {
	}

//	private function applyVisibilityRules()
//	{
//		$vrules = $this->visibilityRules();
//
//		if (empty($vrules))
//			return true;
//
//		$user = Yii::app()->user;
//		$visible = false;
//		foreach($vrules as $rule_array) {
//			if (!isset($rule_array['users']))
//				throw new CException(Yii::t('yii','Widget\'s visibility rule can be only used for "users"'));
//
//			$rule = array_shift($rule_array);
//			$users = $rule_array['users'];
//			if ($rule == 'allow') {
//				if (
//					in_array('*', $users) or in_array($user->name, $users) or
//					($user->isGuest and in_array('?', $users)) or
//					(!$user->isGuest and in_array('@', $users))
//				)
//					$visible = true;
//
//			} else if ($rule == 'deny') {
//				if (
//					in_array('*', $users) or in_array($user->name, $users) or
//					($user->isGuest and in_array('@', $users)) or
//					(!$user->isGuest and in_array('?', $users))
//				)
//					$visible = false;
//			} else {
//				throw new CException(Yii::t('yii','Widget\'s visibility rule "{rule}" is neither "allow", nor "deny"',
//					array('{rule}'=>$rule)));
//			}
//		}
//		return $visible;
//	}

	public function getViewPath()
	{
		$class = new ReflectionClass(get_class($this));
		return dirname($class->getFileName());
	}
	public function init()
	{
		if(!$this->visible)
			return;
		$this->beforePrepareContent();

		echo "<div class=\"{$this->cssClass}\">\n";
		if($this->title!==null)
			echo "<div class=\"{$this->headerCssClass}\">{$this->title}</div>\n";
		echo "<div class=\"{$this->contentCssClass}\">\n";
	}

	public function run()
	{
		if(!$this->visible)
			return;
		$this->renderContent();
		echo "</div><!-- {$this->contentCssClass} -->\n";
		echo "</div><!-- {$this->cssClass} -->";
	}

	protected function prepareContent()
	{
		return null;
	}

	protected function renderContent()
	{
		$data = $this->prepareContent();
		$this->render(get_class($this).'View', $data);
	}
}
?>